<?php

class AdvertiseController extends BackEndController {

    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

    public function accessRules() {
        return array(
            array(
                'allow',
                'actions' => array('login', 'logout', 'test'),
                'users' => array('*'),
            ),
            array(
                'allow',
                'actions' => array('index', 'view', 'create', 'update', 'admin', 'delete',
                    'menu', 'printAll', 'PrintByDate', 'Print',),
                'users' => array('@'),
            ),

            array('deny', // deny all User
                'users' => array('*'),
            ),
        );
    }

    public function actionView($id) {
        $this->render('view', array(
            'model' => $this->loadModel($id),
        ));
    }

    public function actionCreate() {
        $model = new Advertise;

        if (isset($_POST['Advertise'])) {
            $model->attributes = $_POST['Advertise'];
            $model->created_date = date('d-m-Y');
            if ($model->save()) {
                $model->code = "AD$model->id";
                $model->save();
                $this->redirect(array('index'));
            }

        }

        $this->render('create', array(
            'model' => $model,
            'categories' => Category::model()->findAll()
        ));
    }

    public function actionUpdate($id) {
        $model = $this->loadModel($id);
        if (isset($_POST['Advertise'])) {
            $model->attributes = $_POST['Advertise'];
            if ($model->save())
                $this->redirect(array('index'));
        }

        $this->render('update', array(
            'model' => $model,
            'categories' => Category::model()->findAll()
        ));
    }

    public function actionDelete($id) {
        $this->loadModel($id)->delete();

        // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
        if (!isset($_GET['ajax']))
            $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index'));
    }

    public function actionIndex() {
        $model = new Advertise('search');
        $model->unsetAttributes(); // clear any default values
        if (isset($_GET['Advertise']))
            $model->attributes = $_GET['Advertise'];

        if (isset($_POST['DeleteButton'])) {
            if (isset($_POST['selectedIds'])) {
                foreach ($_POST['selectedIds'] as $id) {
                    $ad = $this->loadModel($id);
                    $ad->delete();
                }
                $this->redirect('index');
            }
        }

        if (isset($_POST['DeleteAllButton'])) {
            $model = Advertise::model()->findAll();
            foreach ($model as $ad) {
                $ad->delete();
            }
            $this->redirect('index');
        }
        $this->render('admin', array(
            'model' => $model,
        ));
    }

    public function loadModel($id) {
        $model = Advertise::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function actionMenu() {
        $this->render('print_menu');
    }

    public function actionPrintAll() {
        $this->layout = '//layouts/print';

        $products = Advertise::model()->findAll(array(
            'select' => '*',
            'order' => 'category_id ASC'
        ));
        $this->render('print', array('model' => $products));
    }

    public function actionPrintByDate() {
        $this->layout = '//layouts/print';
        if (!isset($_GET['date'])) throw new CHttpException(999, 'Invalid request');
        $date = $_GET['date'];

        $criteria = new CDbCriteria();
        $criteria->select = '*';
        $criteria->condition = 'created_date = :date';
        $criteria->params = array('date' => $date);

        $products = Advertise::model()->findAll($criteria);
        $this->render('print', array('model' => $products));
    }

    public function actionPrint() {
        $this->layout = '//layouts/print';
        $date = date('d-m-Y');
        $criteria = new CDbCriteria();
        $criteria->select = '*';
        $criteria->condition = 'created_date = :date';
        $criteria->params = array('date' => $date);

        $products = Advertise::model()->findAll($criteria);
        $this->render('print', array(
            'model' => $products
        ));
    }

    public function actionTest() {
        for ($i = 0; $i <= 30; $i++) {
            $ad = new Advertise();
            $ad->name = 'Tìm người quen';
            $ad->content = 'Tìm người quen Tìm người quen Tìm người quen Tìm người quen Tìm người quen Tìm người quen Tìm người quen Tìm người quen Tìm người quen Tìm người quen ';
            $ad->created_date = date('d-m-Y');
            $ad->category_id = rand(1, 3);
            $ad->save();
            $ad->code = "AD$ad->id";
            $ad->save();
        }
    }
}
